package com.wanchuang.hepos.ui.page.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.protobuf.InvalidProtocolBufferException;
import com.wanchuang.architecture.utils.DateUtil;
import com.wanchuang.architecture.utils.ValidateUtils;
import com.wanchuang.hepos.R;
import com.wanchuang.hepos.bridge.data.bean.ZhanDanBean;
import com.wanchuang.hepos.bridge.data.repository.Apis;
import com.wanchuang.hepos.bridge.data.repository.WebApis;
import com.wanchuang.hepos.bridge.request.HomeRequestViewModel;
import com.wanchuang.hepos.bridge.state.home.ZhanDanFragmentViewModel;
import com.wanchuang.hepos.bridge.state.home.ZhangMingFragmentViewModel;
import com.wanchuang.hepos.databinding.FragmentZhanDanBinding;
import com.wanchuang.hepos.databinding.FragmentZhanMingBinding;
import com.wanchuang.hepos.net.NetworkManager;
import com.wanchuang.hepos.net.interfaces.OnRequestCallBack;
import com.wanchuang.hepos.proto.Statistics;
import com.wanchuang.hepos.ui.base.BaseFragment;
import com.wanchuang.hepos.ui.dialog.SelectDayDialog;
import com.wanchuang.hepos.ui.dialog.SelectMonthDialog;
import com.wanchuang.hepos.ui.dialog.SelectTypeDialog;
import com.wanchuang.hepos.ui.web.ui.WebViewActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class ZhangMingFragment extends BaseFragment {


    private FragmentZhanMingBinding mBinding;
    private ZhangMingFragmentViewModel mViewModel;
    private HomeRequestViewModel mRequestViewModel;
    private List<Statistics.Item> statusList = new ArrayList<>();
    private List<Statistics.Item> fangList = new ArrayList<>();


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(ZhangMingFragmentViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(HomeRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_zhan_ming, container, false);
        mBinding = FragmentZhanMingBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mViewModel.layoutManager.set(new LinearLayoutManager(mActivity));
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 回调错误
        mRequestViewModel.getErrorString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        // 回调成功
        mRequestViewModel.getResultString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });

        // 回调账单信息
        mRequestViewModel.getTradings().observe(this, tradings -> {
            hideProgress();
            String customTime = "";
            if (ValidateUtils.isValidate(mViewModel.data)) {
                customTime = DateUtil.getStrTimeFormat(mViewModel.data.get(mViewModel.data.size() - 1).getTime(), "yyyy-MM-dd");
            }
            List<ZhanDanBean> beans = new ArrayList<>();
            for (Statistics.trading in : tradings) {
                String itemTime = DateUtil.getStrTimeFormat(in.getTime(), "yyyy-MM-dd");
                if (!customTime.equals(itemTime)) {
                    customTime = itemTime;
                    beans.add(new ZhanDanBean(itemTime));
                }
                ZhanDanBean beanNext = new ZhanDanBean(in);
                beans.add(beanNext);
            }
            mViewModel.adapter.get().addData(beans);
            int size = tradings.size();
            if (size < mViewModel.pagesize) {
                mViewModel.adapter.get().getLoadMoreModule().loadMoreEnd();
            } else {
                mViewModel.adapter.get().getLoadMoreModule().loadMoreComplete();
            }
        });


        Statistics.Item.Builder builder = Statistics.Item.newBuilder();
        builder.setTitle("入账");
        builder.setCode("1");
        fangList.add(builder.build());

        Statistics.Item.Builder builder2 = Statistics.Item.newBuilder();
        builder2.setTitle("出账");
        builder2.setCode("2");
        fangList.add(builder2.build());




        Statistics.Item.Builder builde3 = Statistics.Item.newBuilder();
        builde3.setTitle("成功");
        builde3.setCode("1");
        statusList.add(builde3.build());

        Statistics.Item.Builder builder4 = Statistics.Item.newBuilder();
        builder4.setTitle("处理中");
        builder4.setCode("2");
        statusList.add(builder4.build());


        Statistics.Item.Builder builder5 = Statistics.Item.newBuilder();
        builder5.setTitle("失败");
        builder5.setCode("3");
        statusList.add(builder5.build());

        Statistics.Item.Builder builder6 = Statistics.Item.newBuilder();
        builder6.setTitle("退汇");
        builder6.setCode("4");
        statusList.add(builder6.build());





    }

    @Override
    public void loadInitData() {
        super.loadInitData();
        loadData();
    }

    private void loadData() {
        mRequestViewModel.get_trading_list(mViewModel.page, mViewModel.typeNmber, mViewModel.statusNumber,
                mViewModel.year.get() + "-" + mViewModel.month.get(), mViewModel.dayFirst.get(), mViewModel.dayEnd.get());
    }

    public class ClickProxy {
        public void back() {
            nav().navigateUp();
            mSharedViewModel.isHomeLoad.setValue(true);
        }

        public void selectDay() {
            SelectDayDialog.Builder builder = new SelectDayDialog.Builder(mActivity);
            builder.setYearAndMonth(Integer.valueOf(mViewModel.year.get()), Integer.valueOf(mViewModel.month.get()))
                    .setSelectDay(Integer.valueOf(mViewModel.dayFirst.get()), Integer.valueOf(mViewModel.dayEnd.get()))
                    .setOnDialogClickListener((view, dayFirst, dayEnd) -> {
                        mViewModel.dayFirst.set(dayFirst);
                        mViewModel.dayEnd.set(dayEnd);
                        mViewModel.data.clear();
                        mViewModel.page = 1;
                        mViewModel.adapter.get().getLoadMoreModule().setEnableLoadMore(true);
                        showProgress();
                        loadData();
                    });
            mViewModel.dayDialog = builder.build();
            mViewModel.dayDialog.setCancelable(true);
            mViewModel.dayDialog.show(getChildFragmentManager());
        }

        public void selectMonth() {
            if (mViewModel.monthDialog == null) {
                SelectMonthDialog.Builder builder = new SelectMonthDialog.Builder(mActivity);
                builder.setYearAndMonth(Integer.valueOf(mViewModel.year.get()), Integer.valueOf(mViewModel.month.get()))
                        .setOnDialogClickListener((view, month) -> {
                            String[] split = month.split("-");
                            mViewModel.year.set(split[0]);
                            mViewModel.month.set(split[1]);
                            mViewModel.dayFirst.set("1");
                            mViewModel.dayEnd.set(DateUtil.getLastDayOfMonth(Integer.valueOf(split[0]), Integer.valueOf(split[1])));
                            mViewModel.data.clear();
                            mViewModel.page = 1;
                            mViewModel.adapter.get().getLoadMoreModule().setEnableLoadMore(true);
                            showProgress();
                            loadData();
                        });
                mViewModel.monthDialog = builder.build();
                mViewModel.monthDialog.setCancelable(true);
            }
            mViewModel.monthDialog.show(getChildFragmentManager());
        }

        public void selectType() {
            if (fangList.size() == 0) {
                return;
            }
            SelectTypeDialog.Builder builder = new SelectTypeDialog.Builder(mActivity);
            builder.setOnDialogClickListener((view, typeName, statusName, typeNumber, status) -> {
                mViewModel.type.set(typeName);
                mViewModel.typeNmber = typeNumber;
                mViewModel.statusNumber = status;
                mViewModel.data.clear();
                mViewModel.page = 1;
                mViewModel.adapter.get().getLoadMoreModule().setEnableLoadMore(true);
                showProgress();
                loadData();
            });
            builder.setOnDialogFangDate(fangList);
            builder.setOnDialogStatusDate(statusList);
            builder.setFangTypeStatus(mViewModel.typeNmber, mViewModel.statusNumber);
            mViewModel.typeDialog = builder.build();
            mViewModel.typeDialog.setCancelable(true);
            mViewModel.typeDialog.show(getChildFragmentManager());
        }


        public void itemOnClick(int position) {
            ZhanDanBean zhanDanBean = mViewModel.data.get(position);
            if (zhanDanBean.getMytype() == 2)  // 内容栏
            {
                WebViewActivity.startActivity(mActivity, WebApis.billDetails + "?id=" + zhanDanBean.getId() + "&time=" + zhanDanBean.getTime());
            }
        }

        public void loadMoreListener() {
            mViewModel.page = mViewModel.adapter.get().getData().size() / mViewModel.pagesize + 1;
            loadData();
        }
    }
}
